﻿using UnityEngine;
using System.Collections;

public class UILabelFadeOut : MonoBehaviour {

	UILabel lbl;
	public bool bApply;

	void Awake()
	{
		lbl = GetComponent<UILabel>();
	}

	public void Init(Vector3 newPos)
	{
		//transform.localPosition = newPos;
		gameObject.SetActive(true);
		StartCoroutine("NumFadeOut");
	}

	IEnumerator NumFadeOut()
	{
		yield return new WaitForSeconds(0.2f);

		LeanTween.moveLocalY( gameObject, transform.localPosition.y + 20, 0.5f);
		
		yield return new WaitForSeconds(0.2f);
		float alpha = lbl.color.a;
		Color original = lbl.color;
		
		while (alpha > 0)
		{
			//glog.debug("************ pos="+lbl.gameObject.transform.localPosition);
			alpha -= 2 * Time.deltaTime;
			if (alpha < 0)
				alpha = 0;
			lbl.color = new Color(original.r, original.g, original.b, alpha);
			yield return null;
		}
		lbl.color = original;
		gameObject.SetActive(false);
		//ObjectPool.Recycle(this);
	}

	void Update()
	{
		if(bApply)
		{
			Init(Vector3.zero);
			bApply=false;
		}
	}
}
